#include <bits/stdc++.h>
#define int long long
#define PII pair <int, int>
using namespace std;
const int N = 2e5 + 5;
int n, ans1, ans1pos, ans2;
int a[N], dp1[N], pos1[N], dp2[N];
struct Tree {
	PII node[N];
	void add(int x, int k, int p) { for(; x < N; x += x & -x) if(k >= node[x].first) node[x] = {k, p}; }
	PII gsum(int x) {
		if(!x) return {};
		PII res = gsum(x - (x & -x));
		if(node[x].first >= res.first) return node[x];
		else return res;
	}
} tree;
signed main() {
	freopen("shung.in", "r", stdin);
	freopen("shung.out", "w", stdout);
	ios :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i ++ ) cin >> a[i];
	for(int i = 1; i <= n; i ++ ) {
		PII res = tree.gsum(a[i]);
		dp1[i] = res.first + 1, pos1[i] = res.second;
		tree.add(a[i], dp1[i], i);
		if(dp1[i] > ans1) {
			ans1 = dp1[i];
			ans1pos = i;
		}
	}
	for(int i = ans1pos; i; i = pos1[i]) a[i] = 0;
	memset(tree.node, 0, sizeof tree.node);
	for(int i = 1; i <= n; i ++ ) if(a[i]) {
		dp2[i] = tree.gsum(n - a[i] + 1).first + 1;
		tree.add(n - a[i] + 1, dp2[i], i);
		ans2 = max(ans2, dp2[i]);
	}
	cout << ans1 + ans2;
	return 0;
}

